package com.ychs.service;

import com.ychs.dao.CompetitionMapper;
import com.ychs.entity.Competition;
import com.ychs.util.Constants;
import com.ychs.util.DBUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/7/2
 */
public class CompetitionService implements IBaseService<Competition> {
    /**
     * 添加记录
     *
     * @param competitions 记录对象
     * @return 是否成功
     */
    @Override
    public boolean add(Competition competitions) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        CompetitionMapper mapper = session.getMapper(CompetitionMapper.class);
        boolean result = mapper.insert(competitions)== Constants.UPDATE_SUCCESS;
        session.commit();
        DBUtil.close(session);
        return result;
    }

    /**
     * 修改记录
     *
     * @param competitions 修改记录对象
     * @return 成功与否
     */
    @Override
    public boolean modify(Competition competitions) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        CompetitionMapper mapper = session.getMapper(CompetitionMapper.class);
        boolean result = mapper.update(competitions)== Constants.UPDATE_SUCCESS;
        session.commit();
        DBUtil.close(session);
        return result;
    }

    /**
     * 删除记录
     *
     * @param id 待删除记录的主键
     * @return 是否成功
     */
    @Override
    public boolean remove(int id) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        CompetitionMapper mapper = session.getMapper(CompetitionMapper.class);
        boolean result = mapper.delete(id)== Constants.UPDATE_SUCCESS;
        session.commit();
        DBUtil.close(session);
        return result;
    }

    /**
     * 查询单个对象
     *
     * @param id 查询对象的主键
     * @return 查询结果
     */
    @Override
    public Competition searchById(int id) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        CompetitionMapper mapper = session.getMapper(CompetitionMapper.class);
        Competition competitions = mapper.selectById(id);
        session.commit();
        DBUtil.close(session);
        return competitions;
    }

    /**
     * 查询多条记录
     *
     * @param conditions 查询条件
     * @return 查询结果
     */
    @Override
    public List<Competition> search(Map<String, Object> conditions) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        CompetitionMapper mapper = session.getMapper(CompetitionMapper.class);
        List<Competition> competitionsList  = mapper.select(conditions);
        session.commit();
        DBUtil.close(session);
        return competitionsList;
    }

    /**
     * 按照查询多条记录的总条数
     * @param conditions 查询条件
     * @return 查询结果
     */
    @Override
    public int searchTotalNumber(Map<String, Object> conditions) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        CompetitionMapper mapper = session.getMapper(CompetitionMapper.class);
        int count = mapper.selectTotalNumber(conditions);
        session.commit();
        DBUtil.close(session);
        return count;
    }
}
